<?php
/**
 * Created by PhpStorm.
 * User: jocky
 * Date: 29/08/2017
 * Time: 17:36
 */
namespace frontend\controllers;

use common\models\ar\HistoryUserInfoAlter;
use common\models\ar\UserTrade;
use common\models\ar\User;
use common\models\Common;
use common\models\Sms;
use common\models\UserDao;
use frontend\models\OnlyCaptchaValidate;
use yii\web\Controller;

class PasswordController extends Controller
{
    public $layout = 'common-out';

    public function actions()
    {
        return [
            'captcha' => [
                'class' => 'common\actions\CaptchaAction',
                'minLength' => 4,
                'maxLength' => 4,
                'transparent' => true,
                'offset' => 15,
                'foreColor' => 0x04a9f4,
                'backColor' => 0x000000,
                'height' => 50
            ],
        ];
    }

    public function actionIndex()
    {
        return $this->redirect('retrieve');
    }

    public function actionRetrieve()
    {
        return $this->render('index');
    }

    public function actionRetrieveSubmit()
    {
        $req = \Yii::$app->request;
        $password = trim($req->post("password", ''));
        $passwordConfirm = trim($req->post('password_confirm', ''));
        $captcha = trim($req->post("captcha", ''));

        $validate = new OnlyCaptchaValidate();
        $validate->controller = 'password';
        $validate->captcha = $captcha;

        if (!$validate->validate()) {
            exit(json_encode(['code' => 1001, 'msg' => "验证码不正确 "]));
        }

        if (strlen($password) < 6) {
            $code = 1003;
            $msg = '密码长度不能小于6位';
            exit(json_encode(['code' => $code, 'msg' => $msg]));
        }

        if ($password != $passwordConfirm) {
            $code = 1002;
            $msg = '两次密码不一致';
            exit(json_encode(['code' => $code, 'msg' => $msg]));
        }

        $cellphone = trim($req->post("cellphone", ''));
        $check = Common::dataCheck($cellphone, [
            'type' => 3,
            'detail' => [
                'pattern' => '/^1[34578][0-9]{9}$/',
                'info' => '手机号码不正确'
            ],
        ]);
        if ($check['code'] != 1) {
            $code = 1003;
            $msg = $check['msg'];
            exit(json_encode(['code' => $code, 'msg' => $msg]));
        }

        //一个手机号只能绑定一个账号
        /**
         * @var UserTrade $obj
         */
        $obj = User::find()->where(['cellphone' => $cellphone])->one();
        if (!$obj) {
            $code = 1004;
            $msg = '手机号码绑定的账户不存在';
            exit(json_encode(['code' => $code, 'msg' => $msg]));
        }

        // if ($obj->status != 1) {
        //     $code = 0;
        //     $msg = '未知错误';
        //     exit(json_encode(['code' => $code, 'msg' => $msg]));
        // }

        // $smsCode = trim($req->post('smsCode', ''));
        // $check = Sms::smsCodeCheck($cellphone, $smsCode, 4);
        // if ($check['code'] != 1) {
        //     $code = 1005;
        //     $msg = $check['msg'];
        //     exit(json_encode(['code' => $code, 'msg' => $msg]));
        // }

        // $passwordSalt = mt_rand(100000, 999999);
        // var_dump($obj->loginpsw);
        // exit();
        // if ($obj->loginpsw == '') {
        //     $obj->password_salt = (string)$passwordSalt;
        // } else {
        //     $passwordSalt = $obj->password_salt;
        // }

        // $passwordMd5 = md5(md5($password) . $passwordSalt);
        if ($password == $obj->loginpsw) {
            $code = 1006;
            $msg = "未修改 | 新旧密码相同";
            exit(json_encode(['code' => $code, 'msg' => $msg]));
        }

        $return = UserDao::updatePassword($obj->userID, $password);
        if ($return['code'] == 1) {
            $obj->loginpsw = $password;
            $obj->save();

            $code = 1;
            $msg = '重置密码成功';
        } else {
            $code = 0;
            $msg = '重置密码失败';
        }

        // $remark = $_POST;
        // var_dump($remark);
        // exit();
        // $remark['msg'] = $msg;
        // $history = new HistoryUserInfoAlter();
        // $history->attributes = [
        //     'uid_operate' => $obj->uid,
        //     'uid' => $obj->uid,
        //     'type' => 2,
        //     'create_time' => time(),
        //     'ip' => \Yii::$app->request->getUserIP(),
        //     'ua' => \Yii::$app->request->getUserAgent(),
        //     'remark' => json_encode($remark)
        // ];
        // $history->save();

        exit(json_encode(['code' => $code, 'msg' => $msg]));
    }

    /**
     * 发送短信
     */
    public function actionSendSms()
    {
        $cellphone = trim(\Yii::$app->request->get('cellphone', ''));
        $captcha = trim(\Yii::$app->request->get('captcha', ''));

        $validate = new OnlyCaptchaValidate();
        $validate->controller = 'password';
        $validate->captcha = $captcha;

        if (!$validate->validate()) {
            exit(json_encode(['code' => 1001, 'msg' => "验证码不正确 "]));
        }

        $check = Common::dataCheck($cellphone, [
            'type' => 3,
            'detail' => [
                'pattern' => '/^1[34578][0-9]{9}$/',
                'info' => '手机号码不正确'
            ],
        ]);
        if ($check['code'] != 1) {
            $code = 1001;
            $msg = $check['msg'];
            exit(json_encode(['code' => $code, 'msg' => $msg]));
        }


        $return = Sms::send($cellphone, 4);
        exit(json_encode($return));
    }
}